package com.djakapm.swing.common;

import javax.swing.*;
import java.awt.*;

/**
 * This class contain static methods are used to set the position of a component on screen, i.e JFrame.
 */
public class ScreenUtil {
    public static final Dimension SCREEN_DIMENSION = Toolkit.getDefaultToolkit().getScreenSize();;

    private static Point centerPoint(Dimension uiDimension) {
        int x;
        int y;
        x = (SCREEN_DIMENSION.width / 2) - (uiDimension.width / 2);
        y = (SCREEN_DIMENSION.height / 2) - (uiDimension.height / 2);
        return new Point(x, y);
    }

    /**
     * Positioned <code>frame</code> in the center of the screen, regardless its resolution.
     * @param frame target component to be positioned.
     */
    public static void center(JFrame frame) {
        frame.setLocation(centerPoint(frame.getSize()));
    }

    /**
     * Positioned <code>window</code> in the center of the screen, regardless its resolution.
     * @param window target component to be positioned.
     */
    public static void center(JWindow window) {
        window.setLocation(centerPoint(window.getSize()));
    }

    /**
     * Positioned <code>dialog</code> in the center of the screen, regardless its resolution.
     * @param dialog target component to be positioned.
     */
    public static void center(JDialog dialog) {
        dialog.setLocation(centerPoint(dialog.getSize()));
    }


}
